let app = getApp();
const reg = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    isPhone: true,
    flag: false,
    isJoin: false,
    isTime: true,
    interval: 3000,
    duration: 1500,
    activeColor: '#fff',
    admin: false,
    file_list: [
      {fileid:'cloud://online-00000.6f6e-online-00000/image/home/xc1.jpg',max_age:7200},
      {fileid:'cloud://online-00000.6f6e-online-00000/image/home/xc2.jpg',max_age:7200},
      {fileid:'cloud://online-00000.6f6e-online-00000/image/home/xc3.jpg',max_age:7200},
      {fileid:'cloud://online-00000.6f6e-online-00000/image/home/xc3.jpg',max_age:7200},
      {fileid:'cloud://online-00000.6f6e-online-00000/image/home/xc6.jpg',max_age:7200}
    ],
    images:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.loadWxImg(this.data.file_list).then(res=>{
      this.setData({
        images:res
      })
    })
    qq.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        app.callWxFun('getqqOpenid', { js_code: res.code }).then(res => {
          this.setData({
            openid: res.openid
          });
          this.getStatus();
          this.getTotal();
          this.isAdmin();
          app.callWxFun('getStatus', {}).then(res => {
            console.log('报名时间',res);
            this.setData({
              isTime: res.status
            })
          })
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getTotal() {
    let sql = `db.collection('joiner').where({
      _id:_.neq('')
    }).count()`;
    app.countDatabase(sql).then(res => {
      this.setData({
        total: res
      })
    })

  },

  getStatus() {
    app.callWxFun('join', { act: 'check', openid: this.data.openid }).then(res => {
      console.log('是否报名',res);
      this.setData({
        isJoin: res.status
      })
    })
  },

  isAdmin() {
    app.callWxFun('isAdmin', { openid: this.data.openid}).then(res => {
      //console.log(res);
      this.setData({
        admin: res.status
      })
    })
  },

  torec(e) {
    console.log(e);
    qq.navigateTo({
      url: '../record/record',
    })
  },
  radioChange(e) {
    let val = e.detail.value;
    this.setData({
      isPhone: val == '0' ? true : false
    })
  },


  nameFocus: function () {
    this.setData({
      flag: true
    });
  },
  nameBlur: function (e) {
    if (e.detail.value) {
      this.setData({
        name: e.detail.value
      });
    }
    else {
      this.setData({
        flag: false
      });
    }
  },

  toJion(e) {
    //console.log(e);
    const { type } = e.detail.value;
    const formId = e.detail.formId;
    if (type == '0') {
      let phone = e.detail.value.phone;
      if (phone == '' || !reg.test(phone)) {
        wx.showModal({
          title: '提示',
          content: '请输入正确的邮箱',
          showCancel: false
        })
        return;
      }
    }

    wx.showLoading({
      title: '正在报名',
    });

    let data = {
      userInfo: app.globalData.userInfo,
      time: new Date().getTime()
    }
    if (type == '0') {
      let phone = e.detail.value.phone;
      data.phone = phone
    }

    //console.log(data);
    app.callWxFun('join', { formId, data, act: 'join', openid: app.globalData.openid,btype:'qq'}).then(res => {
      wx.hideLoading();
      console.log(res);
      wx.showToast({
        title: res.mes,
        icon: 'none',
        duration: 3000
      });
      this.getTotal();
      this.getStatus();
    }).catch(e => {
      wx.hideLoading()
      console.log(e);
      wx.showToast({
        title: '报名失败,请联系管理员',
        icon: 'none'
      })
    })

  },

  toResult() {
    wx.navigateTo({
      url: '../result/result',
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})